//
//  HomePageLayout.m
//  A段项目
//
//  Created by dllo on 16/1/24.
//  Copyright © 2016年 Scott. All rights reserved.
//

#import "HomePageLayout.h"

@interface HomePageLayout ()

@property (nonatomic, retain) NSMutableArray *arrayForHeght;
@property (nonatomic, retain) NSMutableArray *itemAttribute;
@property (nonatomic, assign) NSInteger numberOfitem;
@property (nonatomic, assign) NSInteger shortestIndex;
@property (nonatomic, assign) NSInteger YForShort;
@property (nonatomic, assign) NSInteger XForShort;

@end

@implementation HomePageLayout

- (void)dealloc {
    
    [self.arrayForHeght release];
    [self.itemAttribute release];
    
    [super dealloc];
    
}

- (void)prepareLayout {
    
    [super prepareLayout];
    
    self.arrayForHeght = [NSMutableArray array];
    self.itemAttribute = [NSMutableArray array];
    
    //添加列高
    [self addColumHeight];
    
    self.numberOfitem = [self.collectionView numberOfItemsInSection:0];
    
    for (int i = 0; i < self.numberOfitem;i++) {
        
        [self searchShortestColum];
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        
        [self setFrame:indexPath];
    }
    
    
}


- (CGSize)collectionViewContentSize {
    
    CGSize contentSize = self.collectionView.frame.size;
    
    NSInteger indexPath = [self searchLongerIndex];
    
    contentSize.height = [self.arrayForHeght[indexPath]floatValue] + 10;
    
    return contentSize;
}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    return self.itemAttribute;
}

- (void)addColumHeight {
    
    for (int i = 0; i < 2; i++) {
        self.arrayForHeght[i] = @(10);
    }
    
}

- (void)searchShortestColum {
    
    CGFloat height = MAXFLOAT;
    NSInteger index = 0;
    
    for (int i = 0; i < 2; i++) {
        
        CGFloat currectHeight = [self.arrayForHeght[i]floatValue];
        if (currectHeight < height) {
            
            height = currectHeight;
            
            index = i;
        }
        
    }
    
    self.shortestIndex = index;
    self.YForShort = height + 10;
    self.XForShort = 10 + (self.itemSize.width + 10) * self.shortestIndex;
    
    
}

- (void)setFrame:(NSIndexPath *)indexPath {
    
    UICollectionViewLayoutAttributes *layoutAttribute = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGFloat height = 0;
    
    height = [self.delegate heightForIndexPath:indexPath];
    
    layoutAttribute.frame = CGRectMake(self.XForShort, self.YForShort, self.itemSize.width, height);
    
    [self.itemAttribute addObject:layoutAttribute];
    
    self.arrayForHeght[self.shortestIndex] = @(self.YForShort + height);
    
    
}

- (NSInteger)searchLongerIndex {
    
    NSInteger indexPath = 0;
    
    CGFloat height = 0;
    
    for (int i = 0; i < 2; i ++) {
        
        CGFloat currentHeight = [self.arrayForHeght[i]floatValue];
        
        if (currentHeight > height) {
            
            height = currentHeight;
            
            indexPath = i;
        }
        
    }
    
    return indexPath;
}

@end
